<template>
  <div class="report-form-content">
    <div>
      <el-tabs v-model="activeName" class="demo-tabs">
        <el-tab-pane label="事前用款申请单" name="first"></el-tab-pane>
        <el-tab-pane label="附件" name="second"></el-tab-pane>
      </el-tabs>
      <div class="tabs-list" v-show="activeName === 'first'">
        <el-row :gutter="20">
          <el-col :sm="24" :lg="29" style="padding-left: 20px">
            <div class="" style="display: flex;align-items: center;">
              <h2>指标信息</h2>
            </div>
            <el-card class="update-log">
              <div class="data-item data-item-1">
                <label>指标编码</label>
                <span>{{ expenditureKpiObj.kpiCode }}</span>
              </div>
              <div class="data-item data-item-1">
                <label>指标名称</label>
                <span>{{expenditureKpiObj.kpiName}}</span>
              </div>
              <div class="report-form-list-content">
                <div class="data-item">
                  <label>预算年度</label>
                  <span>{{ expenditureKpiObj.year }}</span>
                </div>
                <div class="data-item">
                  <label>支出类型</label>
                  <span><DictTag :dict-list="getDictList('ExpenditureKpiPayType')" :dict-key="expenditureKpiObj.payType" /></span>
                </div>
                <div class="data-item">
                  <label>经费类型</label>
                  <span><DictTag :dict-list="getDictList('ExpenditureKpiFundType')" :dict-key="expenditureKpiObj.fundType" /></span>
                </div>
                <div class="data-item">
                  <label>预算总金额</label>
                  <span>￥{{ expenditureKpiObj.amount }}</span>
                </div>
                <div class="data-item">
                  <label>部门</label>
                  <span>{{ showIdLabel(organizationList, expenditureKpiObj.organizationId, 'organizationName') }}</span>
                </div>
                <div class="data-item">
                  <label>功能分类</label>
                  <span>{{ showIdLabel(expenditureFunctionalList, expenditureKpiObj.expenditureFunctionalId, 'name') }}</span>
                </div>
                <div class="data-item data-item-1">
                  <label>财政项目</label>
                  <span>{{ showIdLabel(expenditureFiscalList, expenditureKpiObj.expenditureFiscalId, 'name') }}</span>
                </div>
                <div class="data-item">
                  <label>指标来源</label>
                  <span><DictTag :dict-list="getDictList('ExpenditureKpiSource')" :dict-key="expenditureKpiObj.source" /></span>
                </div>
                <div class="data-item">
                  <label>项目负责人</label>
                  <span>{{ showIdLabel(userList, expenditureKpiObj.managerId, 'name') }}</span>
                </div>
                <div class="data-item">
                  <label>项目类型</label>
                  <span>{{ showIdLabel(expenditurePtypeList, expenditureKpiObj.expenditurePtypeId, 'name') }}</span>
                </div>
              </div>

            </el-card>
            <div class="" style="display: flex;align-items: center;">
              <h2>申请单信息</h2>
            </div>
            <el-card class="update-log">
              <div class="report-form-list-content">
                  <div class="data-item">
                  <label>申请单号</label>
                  <span>{{ obj.applicationCode }}</span>
                </div>
                <div class="data-item">
                  <label>申请日期</label>
                  <span>{{ obj.applicationDate?.substring(0, 10) }}</span>
                </div>
                <div class="data-item">
                  <label>用款申请单类型</label>
                  <span><DictTag :dict-list="getDictList('ExpenditureApplicationType')" :dict-key="obj.applicationType" /></span>
                </div>
                <div class="data-item">
                  <label>申请金额</label>
                  <span>￥{{ obj.amount }}</span>
                </div>
                <div class="data-item">
                  <label>经济分类</label>
                  <span>{{ showIdLabel(expenditureEconomicList, obj.expenditureEconomicId, 'name') }}</span>
                </div>
                <div class="data-item">
                  <label>请款事项</label>
                  <span>{{ obj.items }}</span>
                </div>
                <div class="data-item">
                  <label>支出事由</label>
                  <span>{{ obj.reason }}</span>
                </div>
                <div class="data-item">
                  <label>状态</label>
                  <span>{{ showEnumLabel('ExpenditureApplicationState', obj.state) }}</span>
                </div>
              </div>
              <div class="data-item data-item-1">
                <label>备注</label>
                <span>{{ obj.remark }}</span>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
      <div class="tabs-list" v-show="activeName === 'second'">
        <div class="add-indexs">
          <h2>附件信息</h2>
        </div>
        <el-card class="update-log">
          <div class="report-form-list-content">
            <div class="data-item-1" style="width: 100%;">
              <el-table :data="fileList" style="width: 100%; margin-top: 20px;" row-key="id" border>
                <el-table-column prop="fileType"  label="附件类型">
                  <template v-slot="{row}"><DictTag :dict-list="getDictList('BudgetProjectFileType')" :dict-key="row.fileType" /></template>
                </el-table-column>
                <el-table-column prop="attachmentName" label="附件名称" />
                <el-table-column  label="操作">
                  <template v-slot="{row}">
                    <el-button type="primary" @click="downloadBtn(row)">下载</el-button>
                    <el-button type="primary" @click="prviewdBtn(row)">预览</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
  <el-dialog v-model="attachmentVisible" title="附件预览" width="70%" top="2vh">
    <attachmentPreview v-model:attachmentId="previewAttachmentId" />
    <div style="text-align:right;">
      <el-button type="danger" @click="closeBtn">关闭</el-button>
    </div>
  </el-dialog>
</template>
<script setup>
import {ref, watch} from 'vue'
import {doGet, doGetDownload} from "@/config/http";
import {svc, showIdLabel, getListPageSize, showEnumLabel, getDictList} from "@/config/common";
import attachmentPreview from '@/views/components/base/attachmentPreview.vue'
import initListStore from '@/store/modules/initList'

const props = defineProps({
  expenditureApplicationId: {
    required: true,
    type: String,
  }
})

const emit = defineEmits(['close', 'projectListSelect'])

// tab 切换
let activeName = ref('first')
// 基本数据
let obj = ref({})

// 指标单数据
let expenditureKpiObj = ref({})
// 附件
let fileList = ref([])

let governmentList = ref([])
let organizationList = initListStore().organizationList
let userList = initListStore().userList
let expenditurePtypeList = initListStore().expenditurePtypeList//项目类型
let expenditureFunctionalList = initListStore().expenditureFunctionalList//功能分类
let expenditureEconomicList = initListStore().expenditureEconomicList//经济分类
let expenditureFiscalList = initListStore().expenditureFiscalList//财政项目
let expenditureSubjectList = ref([])//会计科目

let attachmentVisible = ref(false)
let previewAttachmentId = ref(null)

watch(() => props.expenditureApplicationId, async val => {
  await initObj(val)
  // await initList()
},{immediate:true})

 // 获取下拉框数据列表
// async function initList() {
//   let p1 = doGet(svc, '/government/page', {pageSize: getListPageSize()}, false)
//   let p2 = doGet(svc, '/organization/page', {pageSize: getListPageSize()}, false)
//   let p3 = doGet(svc, '/user/page', {pageSize: getListPageSize()}, false)
//   let p4 = doGet(svc, '/expenditure-ptype/page', {pageSize: getListPageSize()}, false)
//   let p5 = doGet(svc, '/expenditure-functional/page', {pageSize: getListPageSize()}, false)
//   let p6 = doGet(svc, '/expenditure-economic/page', {pageSize: getListPageSize()}, false)
//   let p7 = doGet(svc, '/expenditure-fiscal/page', {pageSize: getListPageSize()}, false)
//   let p8 = doGet(svc, '/expenditure-subject/page', {pageSize: getListPageSize()}, false)
//   let resArray = await Promise.all([p1, p2, p3, p4, p5, p6, p7, p8])
//   governmentList.value = resArray[0]['records']
//   organizationList.value = resArray[1]['records']
//   userList.value = resArray[2]['records']
//   expenditurePtypeList.value = resArray[3]['records']
//   expenditureFunctionalList.value = resArray[4]['records']
//   expenditureEconomicList.value = resArray[5]['records']
//   expenditureFiscalList.value = resArray[6]['records']
//   expenditureSubjectList.value = resArray[7]['records']
// }

// 获取数据
async function initObj(id) {
  obj.value = await doGet(svc, '/expenditure-application/' +id, {}, true)
  fileList.value = await doGet(svc, '/expenditure-application-file/listByField', {field: 'expenditureApplicationId', values: id}, false)
  expenditureKpiObj.value = await doGet(svc, '/expenditure-kpi/' + obj.value.expenditureKpiId, {}, false)
}

// 下载
function downloadBtn(row) {
  doGetDownload(svc, '/customAttachment/download/' + row.attachmentId, {})
}
// 预览
function prviewdBtn(row) {
  attachmentVisible.value = true
  setTimeout(() => {
    previewAttachmentId.value = row.attachmentId
  },100)
}
function closeBtn() {
  attachmentVisible.value = false
}
</script>

<style scoped>
.report-form-content-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.file-list-btn {
  display: flex;
  /* display: flex; */
  align-items: center;
  justify-content: space-between
}
.report-form-list-content .el-form-item__label {
  width: 220px !important;
}
.el-form-item__label {
  width: 220px !important;
}
.report-form-list-content {
  display: flex;
  flex-wrap: wrap;
}

.form-label {
  display: inline-flex;
  justify-content: flex-end;
  align-items: flex-start;
  flex: 0 0 auto;
  /* font-size: var(--el-form-label-font-size); */
  font-size: 16px;
  /* color: var(--el-text-color-regular); */
  color: #337ecc;
  height: 32px;
  line-height: 32px;
  padding: 0 12px 0 0;
  box-sizing: border-box;
  width: 200px;
  font-weight: bold;
}
.update-log {
  width: 100%;
  margin-top: 0;
  margin-bottom: 30px;
}
.select-list {
  width: 100%;
  margin: 30px 0;
  display: flex;
  align-items: center;
  /* justify-content: space-between; */
}
.select-list .el-radio-group {
  margin-right: 30px;
}
.select-list .el-button {
  margin-left: 30px;
}
.add-indexs {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.upload-demo {
  margin-right: 20px;
}
.file-list {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 10px 0;
}
.file-list-btns {
  display: flex;
  align-items: center;
}
.file-list-btn-span {
  color: #F56C6C;
  font-size: 12px;
}
.data-item {
  width: 33%;
  display: flex;
  align-items: center;
  overflow: hidden;
  margin: 15px 0;
  font-size: 16px;
  border-bottom: 1px solid #eee;
}
.data-item label {
  width: 280px;
  text-align: right;
}
.data-item span {
  padding-left: 15px;
  display: inline-block;
  width: 500px;
}
.data-item-1 {
  width: 100%;
  align-items:flex-start
}
.data-item-1 label {
  width: 134px !important;
  text-align: right;
}
.data-item-1 span {
  /* padding-left: 15px; */
  display: inline-block;
  width: 1000px;
}
.demo-tabs {
  width: 1024px !important;
}
</style>
